// components/popup/popup.js
import user from '../../../../../utils/user.js'
import global from '../../../../../utils/global'
import {
    inputchange,
    toast
} from '../../../../../utils/util.js'
import {
    register,
    getUserAvatar,
    workInvitationLeader,
    getUserOpenId
} from '../../../../../api/app.js'
const app = getApp()
Component({
    /**
	 * 组件的属性列表
	 */
    properties: {
        invite: Object,
    },

    /**
	 * 组件的初始数据
	 */
    data: {
        user: {
            name: '',
            position: '',
            phone: ''
        },
        showAuthUserStatus: true,
        showAuthPhoneStatus: false,
    },
    ready() {
        if(!user.phoneNum){
            this.setData({
                showAuthPhoneStatus: true,	
            })
        }
        else{
            this.setData({
                ['user.phone']: user.phoneNum
            })
        }
        console.log(this.data.invite,'this.data.invite')
        getUserAvatar({
            userId: this.data.invite.userId
        }).then(res => {
            this.setData({
                avatarUrl: 'data:image/jpeg;base64,' +res
            })
        })
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 获取用户两项授权
        auth() {
            // 获取用户信息授权
            this.setData({
                showAuthUserStatus: true
            })
            // 获取用户手机号授权
            this.setData({
                showAuthPhoneStatus: true
            })
        },
        // 完成用户授权
        authUserComplete() {
            this.setData({
                showAuthUserStatus: false
            })
        },
        // 完成手机授权
        authPhoneComplete() {
            this.setData({
                showAuthPhoneStatus: false
            })
            this.setData({
                'user.phone': user.phoneNum,
            })
        },
        submit() {
            if(!this.data.user.name){
                toast('请输入姓名!')
                return 0
            }
            if(!this.data.user.position){
                toast('请输入职位!')
                return 0
            }
            wx.getUserProfile({
                lang: 'zh_CN',
                desc: '向团队成员展示头像信息',
                success: (res) => {
                    console.log(res)
                    user.avatarUrl = res.userInfo.avatarUrl
                    this.register()
                },
                fail(err) {
                    console.log(err)
                }
            })

        },
        register(){
            console.log(this.data.invite,'this.data.invite')
            register({
                company: {
                    id: this.data.invite.companyId
                },
                user: {
                    userHeadUrl: user.avatarUrl,
                    openId: user.openid,
                    userName: this.data.user.name,
                    phone: user.phoneNum,
                    position: this.data.user.position,
                    createUserId: this.data.invite.userId
                }
            }).then(() => {
                app.toast('注册成功！')
                if(this.data.invite.jobId){
                    console.log(this.data.invite,'inviteinviteinvite成功')
                    wx.login({
                        success:(res)=> {
                            console.log(res,'rescode')
                            getUserOpenId({
                                appId: global.appId,
                                code: res.code
                            }).then(async res=>{
                                console.log(res,'token')
                                global.token = res.token
                                // 加入 管理招聘企业
                                await workInvitationLeader({
                                    jobId:this.data.invite.jobId,
                                    createUserId:this.data.invite.createUserId
                                }).then(()=>{
                                    setTimeout(()=>{
                                        app.toast('您已成功加入')
                                    },500)
                                })
                            })
                        }
                    })
                }
                this.triggerEvent('complete')
            })
        },
        inputchange(event) {
            inputchange(event, this)
        },
        // 加入 管理招聘企业
        // joinWork(){
        //     workInvitationLeader({
        //         jobId:this.data.invite.jobId,
        //         createUserId:this.data.invite.createUserId
        //     }).then(()=>{
        //         app.toast('您已成功加入')
        //     })
        // },
    }
})
